package ws.jonas.sybosc.gui;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.KeyStroke;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

import ws.jonas.sybosc.db.DBConfig;

public class ConfigDialog {
	private static final int TEXTFIELD_WIDTH = 30;
	private static final int FILLER_SIZE = 10;
	private static final String HELP_TEXT = "<b>Database settings.</b><br/>" +
			"<b>Host</b>: Address to database on form: host:port/database_name<br/>" +
			"<b>Vendor</b>: Database vendor (mysql/mssql)<br/>" +
			"<b>User</b>: Database username<br/>" +
			"<b>Password</b>: Database password";

	private final JDialog dialog;
	private final JLabel configPathLabel;
	private final JTextField hostField;
	private final JTextField vendorField;
	private final JTextField userField;
	private final JPasswordField passwordField;

	public ConfigDialog(Frame parent, final DBConfig config) {
		dialog = new JDialog(parent, "Config", true);

		final DBConfig oldConfig = new DBConfig();
		oldConfig.copy(config);

		JButton okButton = new JButton("Ok");
		JButton cancelButton = new JButton("Cancel");
		ActionListener okAction = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				config.setHost(hostField.getText());
				config.setVendor(vendorField.getText().toLowerCase());
				config.setUser(userField.getText());
				config.setPassword(new String(passwordField.getPassword()));
				dialog.setVisible(false);
			}
		};
		okButton.addActionListener(okAction);

		ActionListener cancelAction = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				config.copy(oldConfig);
				dialog.setVisible(false);
			}
		};
		cancelButton.addActionListener(cancelAction);

		JPanel buttonPane = new JPanel();
		buttonPane.add(okButton);
		buttonPane.add(cancelButton);

		JTextPane helpText = new JTextPane();
		helpText.setContentType("text/html");
		helpText.setText(HELP_TEXT);
		helpText.setBackground(UIManager.getColor("Label.background"));
		helpText.setEditable(false);

		JPanel textsPane = new JPanel();
		textsPane.setLayout(new BoxLayout(textsPane, BoxLayout.PAGE_AXIS));
		hostField = new JTextField(config.getHost(), TEXTFIELD_WIDTH);
		vendorField = new JTextField(config.getVendor(), TEXTFIELD_WIDTH);
		userField = new JTextField(config.getUser(), TEXTFIELD_WIDTH);
		passwordField = new JPasswordField(config.getPassword(), TEXTFIELD_WIDTH);

		JTextField[] fields = { hostField, vendorField, userField, passwordField };
		String[] labels = { "Host" , "Vendor", "User", "Password" };
		for (int i = 0; i < fields.length; ++i) {
			JPanel p = new JPanel(new BorderLayout(FILLER_SIZE, FILLER_SIZE));
			p.add(fields[i], BorderLayout.LINE_END);
			p.add(new JLabel(labels[i]), BorderLayout.CENTER);
			textsPane.add(p);
		}

		configPathLabel = new JLabel("Config file: " + DBConfig.CONFIG_FILE);
		configPathLabel.setHorizontalAlignment(JLabel.CENTER);
		configPathLabel.setFont(configPathLabel.getFont().deriveFont(configPathLabel.getFont().getStyle() ^ Font.BOLD));

		JPanel centeringConfigPane = new JPanel(new BorderLayout());
		centeringConfigPane.add(configPathLabel);

		textsPane.add(GuiUtil.createFiller(FILLER_SIZE, FILLER_SIZE));
		textsPane.add(new JSeparator(SwingConstants.HORIZONTAL));
		textsPane.add(GuiUtil.createFiller(FILLER_SIZE, FILLER_SIZE));
		textsPane.add(centeringConfigPane);

		JPanel configPane = new JPanel(new BorderLayout());
		configPane.setBorder(new EmptyBorder(FILLER_SIZE, FILLER_SIZE, FILLER_SIZE, FILLER_SIZE));
		configPane.registerKeyboardAction(cancelAction, KeyStroke.getKeyStroke("ESCAPE"), JComponent.WHEN_IN_FOCUSED_WINDOW);
		configPane.registerKeyboardAction(okAction, KeyStroke.getKeyStroke("ENTER"), JComponent.WHEN_IN_FOCUSED_WINDOW);
		configPane.add(helpText, BorderLayout.PAGE_START);
		configPane.add(buttonPane, BorderLayout.PAGE_END);
		configPane.add(textsPane, BorderLayout.CENTER);

		dialog.setContentPane(configPane);
		dialog.setResizable(false);
		dialog.setLocationRelativeTo(parent);
		dialog.pack();
	}

	public void show() {
		dialog.setVisible(true);
	}
}
